package com.kind.codegen;

import org.apache.ibatis.type.JdbcType;

import java.util.Date;

/**
 * TableModelDesc
 * <p/>
 * <p/>
 * 表格模型描述
 * </P>
 *
 * @version 0.0.1
 */
public class TableColumn {
    // ----------------------------------------------------- Properties

    private String fields;
    private String columnFields;

    /**
     * 字段注释
     */
    private String comment;
    /**
     * 类型
     */
    private String type;
    /**
     * 长度
     */
    private Integer length;
    /**
     * 作用域
     */
    private Integer scale;

    /**
     * 默认值
     */
    private String defaultValue;
    private boolean need = true;

    // ----------------------------------------------------- Constructors

    // ----------------------------------------------------- Methods

    public String getJavaTypeDesc() {
        return getJavaType().getName().split("\\.")[2];
    }

    public Class<?> getJavaType() {
        if (type.equalsIgnoreCase("VARCHAR")) {
            return String.class;
        }
        if (type.equalsIgnoreCase("VARCHAR2")) {
            return String.class;
        } else if (type.equalsIgnoreCase("DATE")) {
            return Date.class;
        } else if (type.equalsIgnoreCase("DATETIME")) {
            return Date.class;
        } else if (type.equalsIgnoreCase("TIMESTAMP")) {
            return Date.class;
        } else if (type.equalsIgnoreCase("CLOB")) {
            return String.class;
        } else if (type.equalsIgnoreCase("DOUBLE")) {
            return Double.class;
        } else if (type.equalsIgnoreCase("NUMBER")) {
            if (scale != 0) {
                return Double.class;
            }
            if (length >= 10) {
                return Long.class;
            }
        } else if (type.equalsIgnoreCase("BIGINT")) {
            return Long.class;
        } else if (type.equalsIgnoreCase("INT UNSIGNED")) {
            return Long.class;
        } else if (type.equalsIgnoreCase("INT")) {
            return Integer.class;
        }

        return String.class;
    }

    public JdbcType getJdbcType() {
        JdbcType jdbcType = JdbcType.VARCHAR;
        if (type.equalsIgnoreCase("VARCHAR2")) {
            jdbcType = JdbcType.VARCHAR;
        } else if (type.equalsIgnoreCase("VARCHAR")) {
            jdbcType = JdbcType.VARCHAR;
        } else if (type.equalsIgnoreCase("DATE")) {
            jdbcType = JdbcType.DATE;
        } else if (type.equalsIgnoreCase("TIMESTAMP")) {
            jdbcType = JdbcType.TIMESTAMP;
        } else if (type.equalsIgnoreCase("CLOB")) {
            jdbcType = JdbcType.CLOB;
        } else if (type.equalsIgnoreCase("INT")) {
            jdbcType = JdbcType.INTEGER;
        } else if (type.equalsIgnoreCase("BIGINT")) {
            jdbcType = JdbcType.BIGINT;
        } else if (type.equalsIgnoreCase("SMALLINT")) {
            jdbcType = JdbcType.INTEGER;
        } else if (type.equalsIgnoreCase("TINYINT")) {
            jdbcType = JdbcType.TINYINT;
        } else if (type.equalsIgnoreCase("NUMBER")) {
            jdbcType = JdbcType.NUMERIC;
        }
        return jdbcType;
    }

    public String getFields() {
        return fields;
    }

    public void setFields(String fields) {
        this.fields = fields;
    }

    public String getColumnFields() {
        return columnFields;
    }

    public void setColumnFields(String columnFields) {
        this.columnFields = columnFields;
    }

    public String getComment() {
        return comment;
    }

    public void setComment(String comment) {
        this.comment = comment;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public Integer getLength() {
        return length;
    }

    public void setLength(Integer length) {
        this.length = length;
    }

    public Integer getScale() {
        return scale;
    }

    public void setScale(Integer scale) {
        this.scale = scale;
    }

    public String getDefaultValue() {
        return defaultValue;
    }

    public void setDefaultValue(String defaultValue) {
        this.defaultValue = defaultValue;
    }

    public boolean isNeed() {
        return need;
    }

    public void setNeed(boolean need) {
        this.need = need;
    }

    @Override
    public String toString() {
        return "TableColumn{" +
                "fields='" + fields + '\'' +
                ", columnFields='" + columnFields + '\'' +
                ", comment='" + comment + '\'' +
                ", type='" + type + '\'' +
                ", length=" + length +
                ", scale=" + scale +
                ", defaultValue='" + defaultValue + '\'' +
                ", need=" + need +
                '}';
    }
}
